﻿namespace Hotel.Module.Management.Models
{
    using System;
    using System.Collections.ObjectModel;
    using Entities = Hotel.Module.Management.DataRepository;
    using System.Collections.Generic;

    public class Order : Model.Infrastructure.IDistributedStorageEntity
    {
        /// <summary>
        /// Creates a new Order instance from Entity order
        /// </summary>
        /// <param name="entity">Entity order object from wich new Order instance will be created</param>
        public static Order FromEnityModel(Entities.Order entity)
        {
            Order result = new Order();
            if (entity == null)
            {
                return null;
            }
            else
            {
                result.Email = entity.Email;
                result.FName = entity.FName;
                result.ID = entity.Id;
                result.InDate = entity.InDate;
                result.LName = entity.LName;
                result.NumberOfPeople = entity.NumberOfPeople;
                result.NumberOfRooms = entity.NumberOfRooms;
                result.OutDate = entity.OutDate;
                result.Phone = entity.Phone;
                result.RoomType = (ItemTypes)entity.RoomType;
                result.State = (OrderState)entity.State;

                foreach (var service in entity.IncludedServices)
                {
                    result.IncludedServices.Add(service.Value);
                }

                foreach (var room in entity.RoomPackages)
                {
                    result.RoomPackage.Add(room.Value);
                }
            }
            return result;
        }
        /// <summary>
        /// Constructor, initializes services and rooms package
        /// </summary>
        public Order()
        {
            m_IncludedServices = new Collection<int>();
            m_RoomPackage = new Collection<int>();
        }

        /// <summary>
        /// NUmber of people if not 1
        /// </summary>
      
        public int NumberOfPeople { get; set; }

        /// <summary>
        /// Number of rooms in order. User can hev 1 and more
        /// </summary>
     
        public int NumberOfRooms { get; set; }

        /// <summary>
        /// bill identification number
        /// </summary>
      
        public int ID { get; set; }

        /// <summary>
        /// User Email
        /// </summary>
     
        public string Email { get; set; }

        /// <summary>
        /// User First name
        /// </summary>
     
        public string FName { get; set; }

        /// <summary>
        /// user llast name
        /// </summary>

        public string LName { get; set; }

        /// <summary>
        /// Users phone
        /// </summary>
       
        public string Phone { get; set; }

        /// <summary>
        /// Current state of the order
        /// </summary>
      
        public OrderState State { get; set; }

        /// <summary>
        /// Services which are included in order(ID`s of servces)
        /// </summary>
      
        public Collection<int> IncludedServices
        {
            get
            {
                return m_IncludedServices;
            }
            set
            {
                m_IncludedServices = value;
            }
        }

        /// <summary>
        /// List of rooms ID`s  that are ordered
        /// </summary>
      
        public Collection<int> RoomPackage
        {
            get { return m_RoomPackage; }
            set { m_RoomPackage = value; }
        }

        /// <summary>
        /// Total Cost to pay for ordered and used services
        /// </summary>
    
        public int? TotalCost { get; set; }


        public string PartitionKey { get; set; }

        public string RowKey { get; set; }

        public DateTime Timestamp { get; set; }

        /// <summary>
        /// In date
        /// </summary>
   
        public DateTime InDate { get; set; }

        /// <summary>
        /// Out date
        /// </summary>
      
        public DateTime OutDate { get; set; }

        /// <summary>
        /// The type of room/hall which ordered
        /// </summary>
        public ItemTypes RoomType { get; set; }

        /// <summary>
        /// Copies a data from current instance to recived object
        /// </summary>
        /// <param name="obj">Object to wich the data of current insctnce will be copied</param>
        public void AssignTo(object obj)
        {
            if (obj is Entities.Order)
            {
                Entities.Order result = obj as Entities.Order;

                result.Email = Email;
                result.FName = FName;
                result.Id = ID;
                result.InDate = InDate;
                result.LName = LName;
                result.NumberOfPeople = NumberOfPeople;
                result.NumberOfRooms = NumberOfRooms;
                result.OutDate = OutDate;
                result.Phone = Phone;
                result.RoomType = (int)RoomType;
                result.State = (int)State;

                List<int> EntityServices = new List<int>();
                foreach (var entityItem in result.IncludedServices)
                {
                    EntityServices.Add(entityItem.Value);
                }

                foreach (var item in IncludedServices)
                {
                    if (!EntityServices.Exists(p => p == item))
                    {
                        Entities.IncludedServices EntityItem = new Entities.IncludedServices();
                        EntityItem.Value = item;
                        result.IncludedServices.Add(EntityItem);
                    }
                }

                List<int> EntityRoomPackage = new List<int>();
                foreach (var entityItem in result.RoomPackages)
                {
                    EntityRoomPackage.Add(entityItem.Value);
                }

                foreach (var item in RoomPackage)
                {
                    if (!EntityRoomPackage.Exists(p => p == item))
                    {
                        Entities.RoomPackage EntityItem = new Entities.RoomPackage();
                        EntityItem.Value = item;
                        result.RoomPackages.Add(EntityItem);
                    }
                }
               
                return;
            }

            throw new NotSupportedException();
        }

        /// <summary>
        /// Services ID`s
        /// </summary>
        private Collection<int> m_IncludedServices;

        /// <summary>
        /// Room package(ID`s of the rooms)
        /// </summary>
        private Collection<int> m_RoomPackage;
    }
}
